package com.weiawei.util;

import java.io.*;
import java.util.Properties;

public class shujuqidongUtils {

    // 读取账号文档
    File file1 = new File("User'sInformation.txt");
    //用properties集合来存储用户的账号数据，并在每次登录 切回主页面时，进行一次初始化，提取数据
    Properties User = new Properties() ;

    public void chushihua() throws IOException {
        if (!file1.exists()) {
            file1.createNewFile();
        }
        BufferedReader br = new BufferedReader(new FileReader(file1));
        User.load(br);
        br.close();
    }


    //返回登录成功与否信息
    public int denglu(String name , String password) {
        if(!User.containsKey(name))
        {//账号不存在
            return 1;
        }
        else if(password.equals(User.get(name)))
        {
         //登录成功
            return 2;
        }else{
         //密码错误
            return 3;
        }
    }


    public int regist(String name, String password) {

        if(User.containsKey(name))
        {
            return 1 ; // 用户已存在
        }else {

            User.put(name,password);
            try {
                FileWriter fw = new FileWriter(file1,true);
                User.store(fw,null);
                fw.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

            return 2 ; // 成功
        }


    }
}

